package tree.simple;

import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 二叉树的最大深度
 * 给定二叉树 [3,9,20,null,null,15,7]，
 *
 *     3
 *    / \
 *   9  20
 *     /  \
 *    15   7
 */
public class MaximumDepthOfBinaryTree_104 {

    public int maxDepth(TreeNode root) {
        if (root == null)
            return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        int depth = 0;
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            while(size-- > 0) {
                TreeNode poll = queue.poll();
                if (poll.left != null)
                    queue.offer(poll.left);
                if (poll.right != null)
                    queue.offer(poll.right);
            }
            depth++;
        }
        return depth;
    }

    public int maxDepthDFS(TreeNode node) {
        if (node == null)
            return 0;
        int leftMax = maxDepthDFS(node.left);
        int rightMax = maxDepthDFS(node.right);
        int max = Math.max(leftMax, rightMax) + 1;
        return max;
    }
}
